import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:x_project/common/config.dart';
import 'package:x_project/res/colors.dart';
import 'package:x_project/res/dimens.dart';

import 'load_image.dart';

/// Created by XieXin on 2020/4/1.
/// 按钮封装
class XButton extends StatelessWidget {
  final double width;
  final double? height;
  final String? text;
  final double? textSize;
  final String? icon;
  final FontWeight fontWeight;
  final VoidCallback? onPressed;
  final Color textColor;
  final Color disabledTextColor;
  final Color? color;
  final Color? disabledColor;
  final EdgeInsetsGeometry? margin;
  final EdgeInsetsGeometry padding;
  final double radius;
  final double? rowSpacing;

  const XButton({
    Key? key,
    this.width = double.infinity,
    this.height,
    this.text,
    this.textSize,
    this.icon,
    this.fontWeight = FontWeight.w500,
    this.onPressed,
    this.textColor = Colors.white,
    this.disabledTextColor = XColors.textColor99,
    this.color,
    this.disabledColor,
    this.margin,
    this.padding = EdgeInsets.zero,
    this.radius = 8.0,
    this.rowSpacing,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: margin,
      height: height ?? Dimens.buttonHeight.h,
      width: width,
      child: ElevatedButton(
        onPressed: onPressed,
        style: ButtonStyle(
          elevation: MaterialStateProperty.all(0),
          textStyle: MaterialStateProperty.all(TextStyle(fontSize: textSize)),
          backgroundColor: MaterialStateProperty.all(onPressed == null
              ? disabledColor ?? Get.theme.disabledColor
              : color ?? Get.theme.primaryColorLight),
          foregroundColor: MaterialStateProperty.all(textColor),
          overlayColor:
              MaterialStateProperty.all(Theme.of(context).primaryColorDark),
          padding: MaterialStateProperty.all(padding),
          shape: MaterialStateProperty.all(
            RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(radius),
            ),
          ),
        ),
        child: Container(
          height: height ?? Dimens.buttonHeight.h,
          width: width,
          alignment: Alignment.center,
          child: TextUtil.isEmpty(icon)
              ? Text(
                  text ?? "无",
                  style: TextStyle(
                    fontSize: textSize ?? 28.sp,
                    fontWeight: fontWeight,
                    height: rowSpacing,
                  ),
                  maxLines: 1,
                  textAlign: TextAlign.center,
                  overflow: TextOverflow.ellipsis,
                )
              : Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    LoadAssetImage(
                      icon ?? "ic_default_image",
                      width: 40.w,
                      height: 40.w,
                      moduleName: Config.getModuleName(),
                    ),
                    SizedBox(width: 12.sp),
                    Text(
                      text ?? "无",
                      style: TextStyle(
                        fontSize: textSize ?? 28.sp,
                        fontWeight: fontWeight,
                        height: rowSpacing,
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ],
                ),
        ),
      ),
    );
  }
}
